package swordoffer.offer04;

public class Solution {
    public static void main(String[] args) {
        int[][] nums = {{1,2,3},{4,5,6}};
        boolean numberIn2DArray = findNumberIn2DArray(nums, 5);
        System.out.println("查找目标值为5的元素的结果是："+numberIn2DArray);
    }
    public static boolean findNumberIn2DArray(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int rows = matrix.length;
        int columns = matrix[0].length;
        int row = 0;
        int column = columns - 1;
        while (row < rows && column >= 0) {
            if (matrix[row][column] == target) {
                return true;
            } else if (matrix[row][column] > target) {
                column--;
            } else {
                row++;
            }
        }
        return false;
    }
}
